package com.icez.title_bar

import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.View
import android.view.ViewTreeObserver
import android.widget.LinearLayout
import android.widget.TextView
import com.icez.title_bar.style.CommonBarStyle

/**
 * 通用头部控件
 * @sample
 * @author Icez
 */
class TitleCommonBar @JvmOverloads constructor(
    context: Context, val attrs: AttributeSet? = null
) : LinearLayout(context, attrs) {

    var leftImageViewResId: Int = 0
    var leftImageViewImgWidth: Int = 0
    var leftImageViewImgHeight: Int = 0
    var leftImageViewWeight: Float = 0f
    var leftImageViewPaddingLeft: Int = 0
    var leftImageViewPaddingRight: Int = 0
    var leftImageViewIsVisible: Boolean = false

    var leftImageRightTextViewStr: String = ""
    var leftImageRightTextViewTextSize: Float = 0f
    var leftImageRightTextViewTextColor: Int = 0
    var leftImageRightTextViewTextWeight: Float = 0f
    var leftImageRightTextViewPaddingLeft: Int = 0
    var leftImageRightTextViewPaddingRight: Int = 0
    var leftImageRightTextViewIsVisible: Boolean = false

    var centerTextViewStr: String = ""
    var centerTextViewTextSize: Float = 0f
    var centerTextViewTextColor: Int = 0
    var centerTextViewWeight: Float = 0f
    var centerTextViewIsVisible: Boolean = false

    var rightImageViewResId: Int = 0
    var rightImageViewImgWidth: Int = 0
    var rightImageViewImgHeight: Int = 0
    var rightImageViewWeight: Float = 0f
    var rightImageViewPaddingLeft:Int = 0
    var rightImageViewPaddingRight:Int = 0
    var rightImageViewIsVisible: Boolean = false

    var rightImageLeftTextViewStr: String = ""
    var rightImageLeftTextViewTextSize: Float = 0f
    var rightImageLeftTextViewTextColor: Int = 0
    var rightImageLeftTextViewWeight: Float = 0f
    var rightImageLeftTextViewPaddingLeft: Int = 0
    var rightImageLeftTextViewPaddingRight: Int = 0
    var rightImageLeftTextViewIsVisible: Boolean = false

    var leftImageView:LinearLayout ?= null
    var rightImageLeftTextView: TextView?= null
    var rightImageView:LinearLayout ?= null

    var mOnRightImageViewListener:OnRightImageViewListener ?= null
    var mOnRightImageLeftTextViewListener:OnRightImageLeftTextViewListener ?= null
    var mOnLeftImageViewListener:OnLeftImageViewListener ?= null

    private var statusHeight = 0

    private var mTitleLinearLayout:LinearLayout ?= null

    init {
        orientation = VERTICAL
        val resId = context.resources.getIdentifier("status_bar_height", "dimen", "android")
        statusHeight = context.resources.getDimensionPixelSize(resId)
        getChildStyleValue()

        removeAllViews()
        addStatusBar()
        mTitleLinearLayout = LinearLayout(context)
        val mTitleLinearLayoutParams = LinearLayout.LayoutParams(
            LayoutParams.MATCH_PARENT,
            LayoutParams.MATCH_PARENT       )
        mTitleLinearLayout?.layoutParams = mTitleLinearLayoutParams

        mTitleLinearLayout?.orientation = HORIZONTAL
        addView(mTitleLinearLayout)


        viewTreeObserver.addOnPreDrawListener(object: ViewTreeObserver.OnPreDrawListener{
            override fun onPreDraw(): Boolean {
                initTitleCommonBar()
                viewTreeObserver.removeOnPreDrawListener(this)
                return true
            }

        })




    }

    /**
     * 设置属性后，请主动执行改方法，刷新UI
     * @sample
     * @author Icez
     */
    fun refreshLayout(){
        initTitleCommonBar()
    }




    fun getChildStyleValue() {
        var typeArray = context.obtainStyledAttributes(attrs, R.styleable.TitleCommonBar)
        leftImageViewResId =
            typeArray.getResourceId(R.styleable.TitleCommonBar_leftImageViewResId, 0)
        leftImageViewImgWidth =
            typeArray.getDimensionPixelSize(R.styleable.TitleCommonBar_leftImageViewImgWidth, 0)
        leftImageViewImgHeight =
            typeArray.getDimensionPixelSize(R.styleable.TitleCommonBar_leftImageViewImgHeight, 0)
        leftImageViewWeight = typeArray.getFloat(R.styleable.TitleCommonBar_leftImageViewWeight, 0f)
        leftImageViewPaddingLeft =
            typeArray.getDimensionPixelSize(R.styleable.TitleCommonBar_leftImageViewPaddingLeft, 0)
        leftImageViewPaddingRight =
            typeArray.getDimensionPixelSize(R.styleable.TitleCommonBar_leftImageViewPaddingRight, 0)
        leftImageViewIsVisible =
            typeArray.getBoolean(R.styleable.TitleCommonBar_leftImageViewIsVisible, false)

        leftImageRightTextViewStr =
            typeArray.getString(R.styleable.TitleCommonBar_leftImageRightTextViewStr) ?: ""
        leftImageRightTextViewTextSize = typeArray.getDimensionPixelSize(
            R.styleable.TitleCommonBar_leftImageRightTextViewTextSize,
            0
        ).toFloat()
        leftImageRightTextViewTextColor =
            typeArray.getColor(R.styleable.TitleCommonBar_leftImageRightTextViewTextColor, 0)
        leftImageRightTextViewTextWeight =
            typeArray.getFloat(R.styleable.TitleCommonBar_leftImageRightTextViewTextWeight, 0f)
        leftImageRightTextViewPaddingLeft = typeArray.getDimensionPixelSize(
            R.styleable.TitleCommonBar_leftImageRightTextViewPaddingLeft,
            0
        )
        leftImageRightTextViewPaddingRight = typeArray.getDimensionPixelSize(
            R.styleable.TitleCommonBar_leftImageRightTextViewPaddingRight,
            0
        )
        leftImageRightTextViewIsVisible =
            typeArray.getBoolean(R.styleable.TitleCommonBar_leftImageRightTextViewIsVisible, false)

        centerTextViewStr = typeArray.getString(R.styleable.TitleCommonBar_centerTextViewStr) ?: ""
        centerTextViewTextSize =
            typeArray.getDimensionPixelSize(R.styleable.TitleCommonBar_centerTextViewTextSize, 0)
                .toFloat()
        centerTextViewTextColor =
            typeArray.getColor(R.styleable.TitleCommonBar_centerTextViewTextColor, 0)
        centerTextViewWeight =
            typeArray.getFloat(R.styleable.TitleCommonBar_centerTextViewWeight, 0f)
        centerTextViewIsVisible =
            typeArray.getBoolean(R.styleable.TitleCommonBar_centerTextViewIsVisible, false)
        
        rightImageViewResId =
            typeArray.getResourceId(R.styleable.TitleCommonBar_rightImageViewResId, 0)
        rightImageViewImgWidth =
            typeArray.getDimensionPixelSize(R.styleable.TitleCommonBar_rightImageViewImgWidth, 0)
        rightImageViewImgHeight =
            typeArray.getDimensionPixelSize(R.styleable.TitleCommonBar_rightImageViewImgHeight, 0)
        rightImageViewWeight =
            typeArray.getFloat(R.styleable.TitleCommonBar_rightImageViewWeight, 0f)
        rightImageViewPaddingLeft =
            typeArray.getDimensionPixelSize(R.styleable.TitleCommonBar_rightImageViewPaddingLeft, 0)
        rightImageViewPaddingRight =
            typeArray.getDimensionPixelSize(R.styleable.TitleCommonBar_rightImageViewPaddingRight, 0)
        rightImageViewIsVisible =
            typeArray.getBoolean(R.styleable.TitleCommonBar_rightImageViewIsVisible, false)

        rightImageLeftTextViewStr =
            typeArray.getString(R.styleable.TitleCommonBar_rightImageLeftTextViewStr) ?: ""
        rightImageLeftTextViewTextSize = typeArray.getDimensionPixelSize(
            R.styleable.TitleCommonBar_rightImageLeftTextViewTextSize,
            0
        ).toFloat()
        rightImageLeftTextViewTextColor =
            typeArray.getColor(R.styleable.TitleCommonBar_rightImageLeftTextViewTextColor, 0)
        rightImageLeftTextViewWeight =
            typeArray.getFloat(R.styleable.TitleCommonBar_rightImageLeftTextViewWeight, 0f)
        rightImageLeftTextViewPaddingLeft = typeArray.getDimensionPixelSize(
            R.styleable.TitleCommonBar_rightImageLeftTextViewPaddingLeft,
            0
        )
        rightImageLeftTextViewPaddingRight = typeArray.getDimensionPixelSize(
            R.styleable.TitleCommonBar_rightImageLeftTextViewPaddingRight,
            0
        )
        rightImageLeftTextViewIsVisible =
            typeArray.getBoolean(R.styleable.TitleCommonBar_rightImageLeftTextViewIsVisible, false)
        typeArray.recycle()
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)

        val height = MeasureSpec.getSize(heightMeasureSpec) + statusHeight
        val width = MeasureSpec.getSize(widthMeasureSpec)
        setMeasuredDimension(width, height)

    }

    override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
        super.onLayout(changed, l, t, r, b)
        getChildAt(1).layout(0, statusHeight, r, height)
        Log.e("icez","height: ${height}")

    }



    fun addStatusBar() {
        val view = View(context)

        val layoutParams =
            LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, statusHeight)
        view.layoutParams = layoutParams
        addView(view)
    }

    override fun onAttachedToWindow() {
        super.onAttachedToWindow()

    }

    fun initTitleCommonBar() {

        mTitleLinearLayout?.removeAllViews()

        leftImageView = CommonBarStyle.instance.getLeftImageView(
            context,
            leftImageViewResId,
            leftImageViewImgWidth,
            leftImageViewImgHeight,
            leftImageViewWeight,
            leftImageViewPaddingLeft,
            leftImageViewPaddingRight,
            leftImageViewIsVisible,
            height
        )
        leftImageView?.setOnClickListener {

            mOnLeftImageViewListener?.leftImageViewClick(this)
        }
        mTitleLinearLayout?.addView(
            leftImageView
        )
        mTitleLinearLayout?.addView(
            CommonBarStyle.instance.getLeftImageRightTextView(
                context,
                leftImageRightTextViewStr,
                leftImageRightTextViewTextSize,
                leftImageRightTextViewTextColor,
                leftImageRightTextViewTextWeight,
                leftImageRightTextViewPaddingLeft,
                leftImageRightTextViewPaddingRight,
                leftImageRightTextViewIsVisible,
                height
            )
        )
        mTitleLinearLayout?.addView(
            CommonBarStyle.instance.getCenterTextView(
                context,
                centerTextViewStr,
                centerTextViewTextSize,
                centerTextViewTextColor,
                centerTextViewWeight,
                centerTextViewIsVisible,
                height
            )
        )

        rightImageLeftTextView = CommonBarStyle.instance.getRightImageLeftTextView(
            context,
            rightImageLeftTextViewStr,
            rightImageLeftTextViewTextSize,
            rightImageLeftTextViewTextColor,
            rightImageLeftTextViewWeight,
            rightImageLeftTextViewPaddingLeft,
            rightImageLeftTextViewPaddingRight,
            rightImageLeftTextViewIsVisible,
            height
        )
        rightImageLeftTextView?.setOnClickListener {
            mOnRightImageLeftTextViewListener?.rightImageLeftTextViewClick(this)
        }
        mTitleLinearLayout?.addView(
            rightImageLeftTextView
        )

        rightImageView = CommonBarStyle.instance.getRightImageView(
            context,
            rightImageViewResId,
            rightImageViewImgWidth,
            rightImageViewImgHeight,
            rightImageViewWeight,
            rightImageViewPaddingLeft,
            rightImageViewPaddingRight,
            rightImageViewIsVisible,
            height
        )
        rightImageView?.setOnClickListener {
            mOnRightImageViewListener?.rightImageViewClick(this)
        }
        mTitleLinearLayout?.addView(
            rightImageView
        )



    }


    interface OnRightImageViewListener {
        fun rightImageViewClick(view: View)
    }

    interface OnRightImageLeftTextViewListener {
        fun rightImageLeftTextViewClick(view: View)
    }

    interface OnLeftImageViewListener {
        fun leftImageViewClick(view: View)
    }

}